﻿#include "vtkTipsCoordinate.h"

#include <vtkNamedColors.h>
#include <vtkPlaneSource.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkVectorText.h>
#include <vtkShaderProperty.h>
#include <vtkAxesActor.h>
#include <vtkCaptionActor2D.h>
#include <vtkTextProperty.h>
#include <vtkTextActor.h>
#include "Core/MainWindow.h"

vtkStandardNewMacro(vtkTipsCoordinate);

vtkTipsCoordinate::~vtkTipsCoordinate()
{
    for (auto& actor : pickableActors)
    {
        actor->Delete();
    }
}

vtkTipsCoordinate::vtkTipsCoordinate()
{
    axesActor = vtkSmartPointer<vtkAxesActor>::New();
    // 轴的长度
    axesActor->SetTotalLength(0.5, 0.5, 0.5);
    // 箭头半径
    axesActor->SetConeRadius(0.2f);
    // 轴半径
    axesActor->SetShaftTypeToCylinder();
    axesActor->SetCylinderRadius(0.02);
    axesActor->SetPickable(false);

    // 轴标签
    axesActor->SetAxisLabels(1);
    auto* xAxisCaption = axesActor->GetXAxisCaptionActor2D();
    auto* yAxisCaption = axesActor->GetYAxisCaptionActor2D();
    auto* zAxisCaption = axesActor->GetZAxisCaptionActor2D();
    // 设置标签字体大小
	xAxisCaption->GetTextActor()->SetTextScaleModeToNone();
    xAxisCaption->GetCaptionTextProperty()->SetFontSize(15);
    yAxisCaption->GetTextActor()->SetTextScaleModeToNone();
    yAxisCaption->GetCaptionTextProperty()->SetFontSize(15);
    zAxisCaption->GetTextActor()->SetTextScaleModeToNone();
    zAxisCaption->GetCaptionTextProperty()->SetFontSize(15);

	// 颜色
	/*xAxisCaption->GetCaptionTextProperty()->SetColor(0, 1, 0);
	yAxisCaption->GetCaptionTextProperty()->SetColor(0, 0, 1);
	zAxisCaption->GetCaptionTextProperty()->SetColor(1, 0, 0);*/

    // 左下角全局坐标系
    axesWidget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
	globalAxesActor = vtkSmartPointer<vtkAxesActor>::New();
	
    globalAxesActor->SetTotalLength(2, 2, 2);
    globalAxesActor->SetShaftTypeToCylinder();
    globalAxesActor->SetCylinderRadius(0.5 * globalAxesActor->GetCylinderRadius());  //轴的粗细
    globalAxesActor->SetConeRadius(1.025 * globalAxesActor->GetConeRadius());        //圆锥的大小
    globalAxesActor->SetSphereRadius(1.5 * globalAxesActor->GetSphereRadius());
    globalAxesActor->SetPickable(false);

	vtkTextProperty* tprop = globalAxesActor->GetXAxisCaptionActor2D()->GetCaptionTextProperty();
	tprop->ItalicOn();  //启用文本斜体
	tprop->ShadowOn();  //启用文本阴影
	tprop->SetFontFamilyToTimes();  //字体
	tprop->SetColor(1, 1, 1);  //标签文字颜色
    globalAxesActor->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);
    globalAxesActor->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);
}

void vtkTipsCoordinate::SetRenderer(vtkSmartPointer<vtkRenderer> renderer, vtkSmartPointer<vtkRenderWindowInteractor> renderInteractor)
{
    m_renderer = renderer;
    m_renderInteractor = renderInteractor;
}

void vtkTipsCoordinate::AddToRenderer()
{
    if (!m_renderer || !m_renderInteractor)
    {
        return;
    }

    m_renderer->AddActor(axesActor);
	
    axesWidget->SetOrientationMarker(globalAxesActor);
    axesWidget->SetViewport(0.0, 0.0, 0.2, 0.2);
    axesWidget->SetOutlineColor(1, 0, 0);
    axesWidget->SetInteractor(m_renderInteractor);
    axesWidget->EnabledOn();
    axesWidget->SetInteractive(false);
}

void vtkTipsCoordinate::HandlePickable(MainWindow* m_mainWindows)
{
    pickableActors.push_back(m_mainWindows->createLine(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, 0.48)));
	pickableActors.push_back(m_mainWindows->createLine(gp_Pnt(0, 0, 0), gp_Pnt(0.48, 0, 0)));
	pickableActors.push_back(m_mainWindows->createLine(gp_Pnt(0, 0, 0), gp_Pnt(0, 0.48, 0)));

    //for (auto actor : pickableActors)
    //{
    //    actor->Actor()->GetProperty()->SetLineWidth();
    //}
}
